#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/ioctl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "demo.h"

#include <chrono> 
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
    int test_cnt = 10000;
    if (argc == 2)
    {
	test_cnt = std::stoi(argv[1]);
    }

    int fd = open("/dev/hello", O_RDWR, S_IRUSR|S_IWUSR);
    if(fd != -1)
    {
        static std::chrono::steady_clock::time_point test_start_time = std::chrono::steady_clock::now();
        for (int i = 0; i < test_cnt; i++)
        {
                ioctl(fd, HELLO_IOCTL_INC_NUM, NULL);
        }
        std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
        auto time_span = std::chrono::duration_cast<std::chrono::milliseconds>(now - test_start_time);
        auto time_count = time_span.count();
        std::cout<< test_cnt << " times ioctl elapsed " << time_count << "ms" << std::endl;
        std::cout<< std::endl;
        ioctl(fd, HELLO_IOCTL_PRINT_NUM, NULL);

        ioctl(fd, HELLO_IOCTL_DELAYTEST, 5000);
        std::cout<< "return from delay" << std::endl;

        close(fd);
    }
    else
    {
        perror("Failed at open():");
    }
    return 0;
}

